package org.example;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URL;

public class MenuPanel extends JPanel {
    private SnakeGame main;

    public MenuPanel(org.example.SnakeGame main) {
        this.main = main;
        setLayout(new BorderLayout());
        setPreferredSize(new Dimension(800, 600));

        // 标题
        JLabel titleLabel = new JLabel("贪吃蛇游戏", JLabel.CENTER);
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 48));
        titleLabel.setForeground(new Color(100, 200, 255));
        titleLabel.setBorder(BorderFactory.createEmptyBorder(80, 0, 150, 0));
        add(titleLabel, BorderLayout.NORTH);

        // 按钮面板
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridLayout(3, 1, 0, 30));
        buttonPanel.setOpaque(false);
        buttonPanel.setBorder(BorderFactory.createEmptyBorder(0, 200, 100, 200));

        // 按钮样式
        String[] buttonLabels = {"进入游戏", "游戏设置", "退出游戏"};
        Color[] buttonColors = {new Color(70, 180, 80), new Color(80, 150, 220), new Color(200, 80, 80)};

        //  游戏设置
        JTabbedPane tabbedPane = new JTabbedPane();
        // 1. 游戏设置面板
        URL imageUrl = getClass().getResource("/img/e.jpg");
        ImageIcon icon = new ImageIcon(imageUrl);
        JPanel gamePanel = new JPanel(new GridLayout(4, 2, 10, 10)) {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
             //   g.drawImage(icon.getImage(), 0, 0, getWidth(), getHeight(), this);
            }
        };
        gamePanel.setOpaque(false); // 确保背景可见
        gamePanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        gamePanel.add(new JLabel("音量大小:"));
        JSlider speedSlider = new JSlider(1, 10, 5);
        gamePanel.add(speedSlider);

        gamePanel.add(new JLabel("蛇身颜色:"));
        JButton colorButton = new JButton("选择颜色");
        gamePanel.add(colorButton);


        // 3. 帮助面板
        JPanel helpPanel = new JPanel(new BorderLayout());
        JTextArea helpText = new JTextArea(
                "贪吃蛇游戏帮助\n\n" +
                        "1. 使用方向键或WASD控制蛇的移动\n" +
                        "2. 吃到食物可以增加长度和得分\n" +
                        "3. 碰到墙壁或自身则游戏结束\n\n" ,
                10, 30
        );
        helpText.setEditable(false);
        helpText.setLineWrap(true);
        helpText.setWrapStyleWord(true);
        helpPanel.add(new JScrollPane(helpText), BorderLayout.CENTER);

        // 添加选项卡

        tabbedPane.addTab("游戏设置",null, gamePanel, "游戏基本设置");
        tabbedPane.addTab("帮助", null, helpPanel, "游戏帮助文档");



        for (int i = 0; i < 3; i++) {
            JButton button = new JButton(buttonLabels[i]);
            button.setFont(new Font("微软雅黑", Font.BOLD, 24));
            button.setBackground(buttonColors[i]);
            button.setForeground(Color.WHITE);
            button.setFocusPainted(false);
            button.setBorder(BorderFactory.createRaisedBevelBorder());
            button.setPreferredSize(new Dimension(300, 60));

            // 悬停效果
            int finalI = i;
            button.addMouseListener(new MouseAdapter() {
                public void mouseEntered(MouseEvent e) {
                    button.setBackground(button.getBackground().brighter());
                }
                public void mouseExited(MouseEvent e) {
                    button.setBackground(buttonColors[finalI]);
                }
            });

            // 按钮动作
            switch(i) {
                case 0:
                    button.addActionListener(e -> main.showPrepare());
                    break;
                case 1:
                    button.addActionListener(e -> JOptionPane.showMessageDialog(main,
                            tabbedPane,
                            "游戏说明",
                            JOptionPane.INFORMATION_MESSAGE));
                    break;
                case 2:
                    button.addActionListener(e -> System.exit(0));
                    break;
            }

            buttonPanel.add(button);
        }

        add(buttonPanel, BorderLayout.CENTER);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        // 绘制背景
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        // 渐变背景
        GradientPaint gradient = new GradientPaint(0, 0, new Color(40, 40, 70), getWidth(), getHeight(), new Color(20, 20, 40));
        g2d.setPaint(gradient);
        g2d.fillRect(0, 0, getWidth(), getHeight());

        // 装饰性网格
        g2d.setColor(new Color(255, 255, 255, 20));
        for (int i = 0; i < getWidth(); i += 25) {
            g2d.drawLine(i, 0, i, getHeight());
        }
        for (int i = 0; i < getHeight(); i += 25) {
            g2d.drawLine(0, i, getWidth(), i);
        }

        // 装饰性蛇形图案
        g2d.setColor(new Color(100, 200, 100, 100));
        int[] snakeX = {100, 120, 140, 160, 180, 200, 180, 160, 140, 120, 100, 80, 60, 40, 60, 80};
        int[] snakeY = {100, 80, 100, 80, 100, 120, 140, 160, 180, 160, 180, 160, 180, 160, 140, 120};
        for (int i = 0; i < snakeX.length - 1; i++) {
            g2d.fillOval(snakeX[i], snakeY[i], 25, 25);
        }
    }
}